package services

import (
	"errors"
	"hft/Database/admin"
	"hft/e"
	"hft/global"
)

type CommonActivity struct{}

func (*CommonActivity) CreateActivity(data *admin.CommonActivity) (err error) {

	if res := global.OC_DB.Where("title = ? AND code = ?", data.Title, data.Code).FirstOrCreate(&data); res.RowsAffected == 0 || res.Error != nil {
		return errors.New(e.ActivityHasExist)
	}
	return nil
}

func (*CommonActivity) EditActivity(data *admin.CommonActivity) (err error) {

	if res := global.OC_DB.Model(&admin.CommonActivity{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"code":               data.Code,
		"cover":              data.Cover,
		"title":              data.Title,
		"cont":               data.Cont,
		"button_color":       data.ButtonColor,
		"button_name":        data.ButtonName,
		"button_logo":        data.ButtonLogo,
		"button_bottom_name": data.ButtonBottomName,
		"out_size_cover":     data.OutSizeCover,
		"success_button":     data.SuccessButton,
	}); res.Error != nil || res.RowsAffected == 0 {
		return errors.New(e.ActivityHasExist)
	}
	return nil
}

func (*CommonActivity) GetActivityList() (data []*admin.CommonActivity, err error) {

	var list []*admin.CommonActivity

	global.OC_DB.Find(&list)

	return list, nil
}

func (*CommonActivity) Delete(ids []int) (err error) {
	if len(ids) != 0 {
		err = global.OC_DB.Unscoped().Delete(&admin.CommonActivity{}, ids).Error
		if err != nil {
			return errors.New(e.ErrorInfo)
		}
		return nil
	}
	return nil
}

var CommonActivityService = new(CommonActivity)
